package com.choosefine.base.attendance.client.mq

import com.choosefine.base.common.model.ReturnResult
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.activemq.command.ActiveMQQueue
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.jms.core.JmsMessagingTemplate
import org.springframework.stereotype.Component
import javax.jms.JMSException
import javax.jms.Queue

/**
 * Created by 邱鹏 on 2017/8/30.
 */
@Component
class AttendanceProducer {
    @Autowired lateinit var jmsMessagingTemplate: JmsMessagingTemplate
    @Autowired lateinit var queue: Queue
    @Autowired lateinit var jsonObjectMapper: ObjectMapper
    @Bean
    fun queue(): Queue {
        return ActiveMQQueue("com.choosefine.base.attendance.record")
    }

    open fun ReturnResult.toJSON(): String {
        return jsonObjectMapper.writeValueAsString(this)
    }

    @Throws(JMSException::class)
    fun send(messager: ReturnResult) {
        send(messager.toJSON())
    }

    @Throws(JMSException::class)
    fun send(msg: String) {
        this.jmsMessagingTemplate!!.convertAndSend(this.queue, msg)
    }
}